﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using DAL.Models;
using DAL.BAL;
using DevExpress.Xpf.Editors.Settings;
using DevExpress.Xpf.Grid;
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.Bars;


namespace WpfClient.Windows
{
    /// <summary>
    /// Interaction logic for EmployerEdit.xaml
    /// </summary>
    public partial class EmployerEdit : Window
    {
        private Employer employer = null;

        public EmployerEdit(Employer employer, bool isNew)
        {
            InitializeComponent();

            this.employer = employer;

            if (isNew)
                this.Title = "Новый роботодаьель";
            else
                this.Title = employer.EmployerName;

            if (!Business.CanEditFieldManager())
            {
                if (isNew)
                    this.employer.User = Business.currentUser;

                cbManager.IsReadOnly = true;
            }

            this.DataContext = this.employer;

            
            ((ComboBoxEditSettings)gridContactPersons.Columns["Candidate"].EditSettings).ItemsSource = from n in Business.GetCandidates()
                                                                                                       where n.IsContactPerson == true
                                                                                                       orderby n.LastName
                                                                                                       select n;

            ((ComboBoxEditSettings)gridCommodityGroups.Columns["CommodityGroup"].EditSettings).ItemsSource = Business.GetCommodityGroups();

            cbManager.ItemsSource = Business.GetUsers();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if ((this.employer.EmployerName != null) && (this.employer.EmployerName != string.Empty) && (this.employer.User != null))
            {
                this.DialogResult = true;

                this.Close();
            }
            else
                MessageBox.Show(WpfClient.Properties.Resources.ThereAreMandatoryFields, WpfClient.Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Warning);

        }

        private void teEmployer_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
        {
            if (e.Value == null || e.Value.ToString() == string.Empty)
            {
                e.IsValid = false;

                e.ErrorContent = WpfClient.Properties.Resources.Field_Cant_Be_Empty;
            }
        }

        private void bDeleteCP_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (MessageBox.Show(WpfClient.Properties.Resources.AreYouSureYouWantDelete,
                  WpfClient.Properties.Resources.Warning, MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {

                gridContactPersons.View.CommitEditing();

                DAL.Models.Employer_ContactPersons ec = (DAL.Models.Employer_ContactPersons)gridContactPersons.GetRow(gridContactPersons.View.FocusedRowHandle);

                (gridContactPersons.View as GridViewBase).DeleteRow(gridContactPersons.View.FocusedRowHandle);

                Business.DeleteObject(ec);
                    
            }

        }

        private void cbManager_Validate(object sender, ValidationEventArgs e)
        {
            if (e.Value == null)
            {
                e.IsValid = false;

                e.ErrorContent = WpfClient.Properties.Resources.Field_Cant_Be_Empty;
            }
        }

        private void deleteRowItemCG_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (MessageBox.Show(WpfClient.Properties.Resources.AreYouSureYouWantDelete,
                    WpfClient.Properties.Resources.Warning, MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {

                gridCommodityGroups.View.CommitEditing();

                DAL.Models.Employer_CommodityGroups ec = (DAL.Models.Employer_CommodityGroups)gridCommodityGroups.GetRow(gridCommodityGroups.View.FocusedRowHandle);

                (gridCommodityGroups.View as GridViewBase).DeleteRow(gridCommodityGroups.View.FocusedRowHandle);

                Business.DeleteObject(ec);

            }

        }

        private void gridContactPersons_CustomUnboundColumnData(object sender, GridColumnDataEventArgs e)
        {
            if (e.Column.FieldName == "Phones")
            {
                Candidate key = (Candidate)e.GetListSourceFieldValue("Candidate");

                if (key != null)
                {
                    if (e.IsGetData)
                    {
                        string _phones = string.Empty;

                        var p = from n in Business.dosvidEntities.Candidat_Phones
                                where n.CandidatID == key.Id
                                select n;

                        foreach (Candidat_Phones cp in p)
                        {
                            _phones += string.Format("{0};", cp.Phone);
                        }

                        e.Value = _phones;
                    }
                }
            }
        }

        private void gridContactPersons_ValidateRow(object sender, GridRowValidationEventArgs e)
        {
            if (e.Row == null || ((DAL.Models.Employer_ContactPersons)e.Row).Candidate == null)
            {
                e.ErrorContent = WpfClient.Properties.Resources.Field_Cant_Be_Empty + " (" +
                                 gridContactPersons.Columns["Candidate"].Header + ")";
                e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Warning;
                e.IsValid = false;
            }
            else
                e.IsValid = true;
        }

        private void TableView_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
        }

        private void gridCommodityGroups_ValidateRow(object sender, GridRowValidationEventArgs e)
        {
            if (((DAL.Models.Employer_CommodityGroups)e.Row).CommodityGroup == null) 
            {
                e.ErrorContent = WpfClient.Properties.Resources.Field_Cant_Be_Empty + " (" +
                                 gridCommodityGroups.Columns["CommodityGroup"].Header + ")";
                e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Warning;
                e.IsValid = false;
            }
            else
                e.IsValid = true;
        }
 
    }
}
